'use strict';

/**
 * @name common router
 * @description common router
 * @author yud
 */

angular.module('metalabPartnerApp').
config(function ($stateProvider, $urlRouterProvider, $provide) {
    $provide.decorator('$uiViewScroll', function () {
        return function (uiViewElement) {
            //$('body').animate({ //    scrollTop: uiViewElement.offset().top - 200 //}, 0);
            document.body.scrollTop = uiViewElement.offset().top - 200;
        };
    });

    $stateProvider
        .state('meta', {
            abstract: 'true',
            url: '?officer',
            //reloadOnSearch: false,
            views: {
                '': {
                    templateUrl: 'views/common/structure.html',
                    resolve: {
                        /**
                         * 利用resolve 返回 promise 时 必须加载执行的特点，
                         * 只有检测到登录X-Token后才开始状态的加载
                         * @returns {}[Promise]
                         */
                        passport: function ($rootScope) {
                            return $rootScope.passport.promise;
                        }
                    }
                },
                'header@meta': {
                    resolve: {
                        unreadMessage: function ($rootScope, MetaResources, $q) {
                            var defer = $q.defer();
                            $rootScope.passport.promise.then(function () {
                                MetaResources.message.query({
                                    userId: '_self',
                                    'read': false,
                                    '_range': 0
                                }, function (data, headers) {
                                    defer.resolve(parseInt(headers()['x-count']));
                                }, function (error) {
                                    defer.resolve(error);
                                })
                            });
                            return defer.promise;
                        }
                    },
                    templateUrl: 'views/common/header.html',
                    controller: 'HeaderCtrl'

                },
                'footer@meta': {
                    templateUrl: 'views/common/footer.html'
                }
            },
            data: {
                pageMode: 'normal'
            }
        });

    /**
     * visitor 登录的跳转
     * 一些特殊url的处理
     */
    $urlRouterProvider.when('/', '');
    $urlRouterProvider.when('/demand', '/demand/list');
    $urlRouterProvider.when('/technology', '/technology/list');
    $urlRouterProvider.when('/talent', '/talent/list');
    $urlRouterProvider.otherwise('/');

});
